//
//  MainViewController.m
//  dogGroup
//
//  Created by iOS Two on 14-8-13.
//  Copyright (c) 2014年 iOS Two. All rights reserved.
//

#import "MainViewController.h"
#import "InfoViewController.h"
#import "PicViewController.h"
#import "TrainViewController.h"
#import "DogHospitalViewController.h"
#import "DogFoodViewController.h"
#import "SettingViewController.h"

@interface MainViewController ()

@end

@implementation MainViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}
-(void)viewWillDisappear:(BOOL)animated {
    if (version >= 7) {
        [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"img_navbackground2"] forBarPosition:UIBarPositionAny barMetrics:UIBarMetricsDefault];
    } else {
        [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"img_navbackbule4"] forBarMetrics:UIBarMetricsDefault];
    }
    [MobClick endLogPageView:@"MainViewController"];
}
-(void)viewWillAppear:(BOOL)animated {
    if (version >= 7) {
        [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"img_navbackground"] forBarPosition:UIBarPositionAny barMetrics:UIBarMetricsDefault];
    } else {
        [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"img_navbackpuple4"] forBarMetrics:UIBarMetricsDefault];
    }
    [MobClick beginLogPageView:@"MainViewController"];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    if (version >= 7) { // 判断是否是IOS7
        [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent animated:NO];
    }
    //自定义uinavigationbar 去黑线
    if([UINavigationBar instancesRespondToSelector:@selector(setShadowImage:)]) {
        [[UINavigationBar appearance] setShadowImage:[MainViewController imageWithColor:[UIColor clearColor]
                                                                        size:CGSizeMake(320, 3)]];
    }
    UIImageView *background = [[UIImageView alloc]initWithFrame:self.view.frame];
    background.image = [UIImage imageNamed:@"img_mainbackground1"];
    if (version >= 7) {
        
    } else {
        background.frame = CGRectMake(0, 0, kwidth, kHeight);
    }
    [self.view addSubview:background];
    
    [self creatController];
    if (version >= 7) {
        self.navigationController.navigationBar.barTintColor = [UIColor colorWithRed:0.86 green:0.43 blue:0.12 alpha:1];
        UILabel *titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 100, 30)];
        titleLabel.text = @"萨摩耶养狗指南";
        titleLabel.textAlignment = NSTextAlignmentCenter;
        titleLabel.font = [UIFont systemFontOfSize:20];
        titleLabel.textColor = [UIColor whiteColor];
        self.navigationItem.titleView = titleLabel;
    } else {
        self.navigationController.navigationBar.tintColor = [UIColor colorWithRed:0.86 green:0.43 blue:0.12 alpha:1];
        self.navigationItem.title = @"萨摩耶养狗指南";
    }

    UIBarButtonItem *backItem = [[UIBarButtonItem alloc]init];
    self.navigationItem.backBarButtonItem = backItem;
    backItem.title = @"返回";
    self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
    if (![self netWorkCheck]) {
        [self alertViewShow:@"网络未连接"];
    }
}

-(void)creatController {
    if (kHeight == 568) {
        _orginPoint = [NSArray arrayWithObjects:[NSValue valueWithCGRect:CGRectMake(160, -151.5, 160, 151.5)],
                       [NSValue valueWithCGRect:CGRectMake(0, -151.5, 160, 151.5)],
                       [NSValue valueWithCGRect:CGRectMake(194, -109, 126, 109)],
                       [NSValue valueWithCGRect:CGRectMake(0, -171.5, 194, 171.5)],
                       [NSValue valueWithCGRect:CGRectMake(194, -233.5, 126, 233.5)],
                       [NSValue valueWithCGRect:CGRectMake(0, -171, 194, 171)], nil];
        _lastPoint = [NSArray arrayWithObjects:[NSValue valueWithCGRect:CGRectMake(160, 352.5, 160, 151.5)],
                      [NSValue valueWithCGRect:CGRectMake(0, 352.5, 160, 151.5)],
                      [NSValue valueWithCGRect:CGRectMake(195, 243.5, 126, 109)],
                      [NSValue valueWithCGRect:CGRectMake(0, 181, 194, 171.5)],
                      [NSValue valueWithCGRect:CGRectMake(194, 6, 126, 233.5)],
                      [NSValue valueWithCGRect:CGRectMake(0, 6, 194, 171)], nil];
        _mainImg = [NSArray arrayWithObjects:[UIImage imageNamed:@"img_morefor5"],
                    [UIImage imageNamed:@"img_mainbutton5For5"],
                    [UIImage imageNamed:@"img_mainbutton4For5"],
                    [UIImage imageNamed:@"img_mainbutton3For5"],
                    [UIImage imageNamed:@"img_mainbutton2For5"],
                    [UIImage imageNamed:@"img_mainbutton1For5"], nil];
    } else {
        _mainImg = [NSArray arrayWithObjects:[UIImage imageNamed:@"img_morefor4"],
                    [UIImage imageNamed:@"img_mainbutton5For4"],
                    [UIImage imageNamed:@"img_mainbutton4For4"],
                    [UIImage imageNamed:@"img_mainbutton3For4"],
                    [UIImage imageNamed:@"img_mainbutton2For4"],
                    [UIImage imageNamed:@"img_mainbutton1For4"], nil];
       _orginPoint = [NSArray arrayWithObjects:[NSValue valueWithCGRect:CGRectMake(160, -110.5, 160, 110.5)],
                      [NSValue valueWithCGRect:CGRectMake(0, -110.5, 160, 127)],
                      [NSValue valueWithCGRect:CGRectMake(194, -102.5, 126.5, 102.5)],
                      [NSValue valueWithCGRect:CGRectMake(0, -140, 196.5, 140)],
                      [NSValue valueWithCGRect:CGRectMake(193.5, -198, 126.5, 198)],
                      [NSValue valueWithCGRect:CGRectMake(0, -144, 193.5, 144)], nil];
       _lastPoint = [NSArray arrayWithObjects:[NSValue valueWithCGRect:CGRectMake(160, 302.5, 160, 110.5)],
                     [NSValue valueWithCGRect:CGRectMake(0, 290, 160, 127)],
                     [NSValue valueWithCGRect:CGRectMake(196.5, 200, 126.5, 102.5)],
                     [NSValue valueWithCGRect:CGRectMake(0, 148, 196.5, 140)],
                     [NSValue valueWithCGRect:CGRectMake(193.5, 0, 126.5, 198)],
                     [NSValue valueWithCGRect:CGRectMake(0, 0, 193.5, 144)], nil];
    }
//    _timer = [NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:@selector(createDownyFlake) userInfo:nil repeats:YES];
//    _i = 0;
    for (_i = 0; _i < _lastPoint.count; _i++) {
        [self createDownyFlake];
    }
}

-(void)createDownyFlake {
//    if (_i < _orginPoint.count) {
        UIImageView *imgView = [[UIImageView alloc]initWithFrame:[[_orginPoint objectAtIndex:_i] CGRectValue]];
        imgView.image = [_mainImg objectAtIndex:_i];
        [imgView addGestureRecognizer:[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(buttonClick:)]];
        imgView.tag = _i;
        imgView.userInteractionEnabled = YES;
        [self.view addSubview:imgView];
        
//        [UIView beginAnimations:nil context:(__bridge void *)(imgView)];
//        [UIView setAnimationDuration:0.5];
//        [UIView setAnimationCurve:UIViewAnimationCurveLinear];
//        [UIView setAnimationDelegate:self];
//        [UIView setAnimationDidStopSelector:@selector(animationDidStop:finished:context:)];
        imgView.frame = [[_lastPoint objectAtIndex:_i] CGRectValue];
//        [UIView commitAnimations];
//    } else {
//        [_timer invalidate];
//        _timer = nil;
//        [_timer setFireDate:[NSDate distantFuture]];
//    }
//    _i++;
}
-(void)buttonClick:(UITapGestureRecognizer *)tap {
    InfoViewController *info = [[InfoViewController alloc]init];
    PicViewController *pic = [[PicViewController alloc]init];
    TrainViewController *train = [[TrainViewController alloc]init];
    DogHospitalViewController *hospital = [[DogHospitalViewController alloc]init];
    DogFoodViewController *food = [[DogFoodViewController alloc]init];
    SettingViewController *setting = [[SettingViewController alloc]init];
    switch (tap.view.tag) {
        case 0:
            [self.navigationController pushViewController:setting animated:YES];
            break;
        case 1:
            [self.navigationController pushViewController:hospital animated:YES];
            [MobClick event:@"MainVCHospital"];
            break;
        case 2:
            [self.navigationController pushViewController:food animated:YES];
            [MobClick event:@"MainVCDogFood"];
            break;
        case 3:
            [MobClick event:@"MainVCTrain"];
            [self.navigationController pushViewController:train animated:YES];
            break;
        case 4:
            [MobClick event:@"MainVCImage"];
            [self.navigationController pushViewController:pic animated:YES];
            break;
        case 5:
            [MobClick event:@"MainVCNewComer"];
            [self.navigationController pushViewController:info animated:YES];
            break;
        default:
            break;
    }
}
//动画结束时回调
- (void)animationDidStop:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context {
    
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
